zabbix通过自定义脚本监控nginx,php-fpm和mysql占用内存数和进程的个数
首先,在zabbix脚本目录下添加一个脚本,写入如下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #!/bin/bash top -bn1>/usr/local/zabbix-2.4.4/scripts/process.log LOG=/usr/local/zabbix-2.4.4/scripts/process.log php_fpm(){ grep "php-fpm" $LOG |awk '{sum+=$6}; END{print sum}' } php_fpm_num(){ grep "php-fpm" $LOG |wc -l } nginx(){ grep "nginx" $LOG |awk '{sum+=$6}; END{print sum}' } nginx_num(){ grep "nginx" $LOG |wc -l } mysqld(){ grep "mysqld" $LOG |awk '{sum+=$6}; END{print sum}' } mysqld_num(){ grep "mysqld" $LOG |wc -l } case "$1" in php_fpm) php_fpm ;; php_fpm_num) php_fpm_num ;; nginx) nginx ;; nginx_num) nginx_num ;; mysqld) mysqld ;; mysqld_num) mysqld_num ;; *) echo "Usage: $0 {php_fpm|php_fpm_num|nginx|nginx_num|mysqld|mysqld_num}" esac
|
保存之后修改其属组和属主为zabbix.zabbix,然后新建一个文件process.log,同样设置属主和属组为zabbix.zabbix
然后编辑zabbix_agentd.conf文件,在最后添加上下面这段代码,并重启zabbix_agentd服务
1 2 3 4 5 6
| UserParameter=process.php_fpm,/usr/local/zabbix-2.4.4/scripts/processstatus.sh php_fpm UserParameter=process.php_fpm_num,/usr/local/zabbix-2.4.4/scripts/processstatus.sh php_fpm_num UserParameter=process.nginx,/usr/local/zabbix-2.4.4/scripts/processstatus.sh nginx UserParameter=process.nginx_num,/usr/local/zabbix-2.4.4/scripts/processstatus.sh nginx_num UserParameter=process.mysqld,/usr/local/zabbix-2.4.4/scripts/processstatus.sh mysqld UserParameter=process.mysqld_num,/usr/local/zabbix-2.4.4/scripts/processstatus.sh mysqld_num
|
之后就可以在web页面添加item,生成图表了,注意top取到的内存单位是KB,所以在定义item的时候需要自定义单位和设定倍数
生成的各个程序占用内存大小的图形如下
生成的各个程序的进程个数图形如下,可以设定触发器,当进程满足触发其条件时发送报警